package org.example.sword2offer.primary;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: lynn
 * @Descript: 微信抢红包算法,非公平版，随机的范围不一样，公平算法，随机的范围一样
 * @date: 2021/6/4 8:38
 * @version: 1.0
 */
public class Q67_2_WechatGrabRed {
    public static void main(String[] args) {
        List<Integer> amoutList= divideRedPackage(1000,10);
        for(Integer amout:amoutList){
            System.out.printf("领到的金额:" + new BigDecimal(amout).divide(new BigDecimal(100)));
        }
    }
    public static List<Integer> divideRedPackage(Integer totalAmount,Integer totalPeopleNum){
        List<Integer> amountList=new ArrayList<>();
        Integer restAmount=totalAmount;Integer restPeopleNum=totalPeopleNum;
        Random random= new Random();
        for (int i = 0; i < totalPeopleNum-1 ; i++) {
            //非公平算法，先到领多的概率大，为了防止 restAmount 不够分的情况，加个判断；
            if (restAmount<=0) { amountList.add(0); continue;}
            int amount=random.nextInt(restAmount)+1;
            restAmount -=amount; restPeopleNum --;amountList.add(amount);
        }
        amountList.add(restAmount);
        return amountList;
    }
}
